package com.example.demo.service.impl;
/*
 * @Desc 
 * @author 蔡超
 * @date 2018/9/21 15:12
 */

import com.example.demo.mapper.BdUserInfoMapper;
import com.example.demo.model.BdUserInfo;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    public BdUserInfoMapper bdUserInfoMapper;

    @Override
    public String addUser(BdUserInfo user) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        user.setUserId(uuid);
        String passsword = user.getUserPwd();
        if(StringUtils.isNotBlank(passsword)){
            String salt = "salt-code";
            passsword = new SimpleHash("md5",  passsword, ByteSource.Util.bytes(salt), 2).toHex();
            user.setUserPwd(passsword);
        }
        int result = bdUserInfoMapper.insert(user);
        if(result == 1){
            return user.getUserId();
        }else{
            return null;
        }
    }

    @Override
    public int updateUser(BdUserInfo user) {
        return bdUserInfoMapper.updateByPrimaryKey(user);
    }

    @Override
    public BdUserInfo selectUser(String userId) {
        return bdUserInfoMapper.selectByPrimaryKey(userId);
    }


    @Override
    public List<BdUserInfo> selectUser(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return bdUserInfoMapper.selectAll();
    }

    @Override
    public BdUserInfo getUser(String username) {
        return bdUserInfoMapper.selectUser(username);
    }
}
